package net.sourceforge.peertwitter.chain;

import java.util.HashSet;
import java.util.Set;

import net.sourceforge.peertwitter.chain.exceptions.EventChainInfiniteLoopException;

public class EventChainImpl <E> implements EventChain <E> {
	
	private Set<EventChain<E>> children = new HashSet<EventChain<E>>();

	@Override
	public void addChild(EventChain<E> child) {
		if (child == this)
			throw new EventChainInfiniteLoopException("Cant add itself as a child.");
		children.add(child);
	}

	@Override
	public void deliver(E event) {
		for (EventChain<E> child: children) {
			child.deliver(event);
		}
	}

}
